//
//  Document.m
//  ToolbarTest
//
//  Created by Junjie Gong on 12-5-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Document.h"
#import "myWindowController.h"
#import "AutoDisapearWindowController.h"
#import "BookmarkWindowController.h"
#import "hyperlink_text_view.h"
#import "texture_and_text_button_cell.h"
#include "image_button_cell.h"

@interface Document()

- (void)menuResponse:(id)sender;

@end

@implementation Document

@synthesize scrollView;
@synthesize textView;
@synthesize customButton;
@synthesize hoverButton;

- (id)init
{
    self = [super init];
    if (self) {
        // Add your subclass-specific initialization here.
        // If an error occurs here, return nil.
    }
    return self;
}

-(void)dealloc{
    [scrollView release];
    [textView release];
    [customButton release];
    [hoverButton release];
    [super dealloc];
}


- (void)awakeFromNib{
    [super awakeFromNib];
    

    //[[[NSApp mainWindow] contentView] addSubview:scrollView];
    
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"Document";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    
    NSString *str = [NSString stringWithString:@"龚俊杰"];
    int len1 = str.length;
    
    const char * utf8 = [str UTF8String];
    int len2 = strlen(utf8);
    
    unichar buf[100];
    memset(buf, 0, 100*sizeof(unichar));
    [str getCharacters:buf range:NSMakeRange(0, str.length)];
    //int len3 = strlen(buf);
    
    int row = 4;

//    [textView setMessageAtStart:"hello" withLink:@"www.163.com" end:"ok" font:nil messageColor:[NSColor blueColor] linkColor:[NSColor redColor]];
    
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
    
    
    TextureAndTextButtonCell *cell = (TextureAndTextButtonCell *)customButton.cell;
    [cell setImage:[NSImage imageNamed:@"download_button_bg_light_blue.png"] forButtonState:kDefaultState];
    [cell setImage:[NSImage imageNamed:@"download_button_bg_hover.png"] forButtonState:kPressedState];
    
    ImageButtonCell *cell1 = (ImageButtonCell *)hoverButton.cell;
    [cell1 setImage:[NSImage imageNamed:@"download_button_bg_light_blue.png"] forButtonState:image_button_cell::kDefaultState];
    [cell1 setImage:[NSImage imageNamed:@"download_button_bg_dark_blue.png"] forButtonState:image_button_cell::kHoverState];
    [cell1 setImage:[NSImage imageNamed:@"download_button_bg_hover.png"] forButtonState:image_button_cell::kPressedState];    
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    /*
     Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
    You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    */
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    /*
    Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    */
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return YES;
}

+ (BOOL)autosavesInPlace
{
    return YES;
}

- (IBAction)popupMyWindow:(id)sender{
    NSButton *bt = (NSButton *)sender;
    NSRect f = bt.frame;
    NSPoint p = f.origin;
    NSWindow *mainw = [NSApp mainWindow];
    NSRect mainR = [mainw frame];
    NSPoint mainP = mainR.origin;
    mainP.x += p.x;
    mainP.y += p.y;
    
    myWindowController* ctl = [[myWindowController alloc] init];
    
    mainP.x =  mainP.x - ctl.window.frame.size.width / 2 + f.size.width/2;
    mainP.y -= ctl.window.frame.size.height;
    
    ctl.showPoint = mainP;
    [ctl.window setFrameOrigin:mainP];

    [ctl showWindow:nil];
}

- (IBAction)popupMyWindow1:(id)sender{
    
    AutoDisapearWindowController *ctl = [[AutoDisapearWindowController alloc] initWithWindowNibName:@"AutoDisapearWindow"];
    NSButton *bt = (NSButton *)sender;
    NSRect f = bt.frame;
    NSPoint p = f.origin;
    NSWindow *mainw = [NSApp mainWindow];
    NSRect mainR = [mainw frame];
    NSPoint mainP = mainR.origin;
    mainP.x += p.x;
    mainP.y += p.y;
    mainP.x =  mainP.x - ctl.window.frame.size.width / 2 + f.size.width/2;
    mainP.y -= ctl.window.frame.size.height;
    [ctl.window setFrameOrigin:mainP];
    
    [ctl startShowWindow];
}

- (IBAction)popupMyWindow3:(id)sender{
    BookmarkWindowController *ctl = [[BookmarkWindowController alloc] initWithWindowNibName:@"BookmarkWindow"];
    NSButton *bt = (NSButton *)sender;
    NSRect f = bt.frame;
    ctl.btnRect = f;
    
    [ctl startShowWindow];
}

- (void)menuResponse:(id)sender{
    NSMenuItem *item = (NSMenuItem *)sender;
    NSLog(@"item %d is selected", item.tag);
}

- (IBAction)popupMenu:(id)sender{
    
    NSButton *bt = (NSButton *)sender;
    NSRect f = bt.frame;
    NSPoint p = f.origin;
    NSWindow *mainw = [NSApp mainWindow];
    NSRect mainR = [mainw frame];
    NSPoint mainP = mainR.origin;
    mainP.x += p.x;
    mainP.y += p.y;
    
    NSMenu *menu = [[[NSMenu alloc] init] autorelease];
    //[NSMenu setMenuBarVisible:NO];
    [menu setAutoenablesItems:NO];
    {
        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"58564100" action:@selector(menuResponse:) keyEquivalent:@""];
        item.tag = 1;
        [item setImage:[NSImage imageNamed:@"notice.png"]];
        [menu addItem:item];
        [item release];
    }
    {
        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"龚俊杰" action:@selector(menuResponse:) keyEquivalent:@""];
        item.tag = 2;
        [item setImage:[NSImage imageNamed:@"shop.png"]];
        [menu addItem:item];
        [item release];
    }
    {
        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Users" action:@selector(menuResponse:) keyEquivalent:@""];
        item.tag = 3;
        [item setImage:[NSImage imageNamed:@"user.png"]];
        [menu addItem:item];
        [item release];
    }
    
    {
        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"News" action:@selector(menuResponse:) keyEquivalent:@""];
        item.tag = 4;
        [item setImage:[NSImage imageNamed:@"mail_new.png"]];
        [menu addItem:item];
        [item release];
    }
    
    [menu addItem:[NSMenuItem separatorItem]];
    [menu addItemWithTitle:@"Exit" action:@selector(menuResponse:) keyEquivalent:@""];    
    
    
    mainP.x += 5;
    
    
    BOOL ret = [menu popUpMenuPositioningItem:nil atLocation:mainP inView:nil];
    
    if (ret) {
//        NSLog(@"select menu item ");
    }
    else{
        NSLog(@"cancel menu");
    }
}

@end
